package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.CustomerBOInterface;
import org.cuatrovientos.erp.model.Customer;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form Customer management
 * 
 * @author VB
 */
@Controller
public class CustomerController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(CustomerController.class);

	@Autowired
	private CustomerBOInterface customerBO;

	/**
	 * default constructor
	 */
	public CustomerController() {
		logger.info("Customer BEAN instantiated");
	}

	/**
	 * handles default /customer or /customer/list request
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/customer","/customer/list" )
	 */
	@RequestMapping({ "/customer", "/customer/list" })
	public ModelAndView customerDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Customer> customerList = customerBO.selectAll();
		model.put("customers", customerList);
		return new ModelAndView("CustomerList", model);
	}

	/**
	 * handles /customer/search request, redirected to default page
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/customer/search" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/customer/search" })
	public ModelAndView search(@RequestParam(value = "name", defaultValue = "", required = true) String name) {
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Customer> customerList = customerBO.search(name);
		model.put("customers", customerList);

		return new ModelAndView("CustomerList", model);
	}

	/**
	 * handles delete customer, and redirect to customer default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/customer","/customer/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/customer/delete" })
	public String deleteCustomer(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("delete " + id);

		customerBO.deleteById(id);
		return "redirect:/customer";
	}

	/**
	 * handles new customer request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it: in this case "customer"
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/customer/new" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/customer/new" })
	public ModelAndView newCustomer() {
		logger.info("new customer ");
		Customer customer = new Customer();

		return new ModelAndView("NewCustomer", "customer", customer);
	}

	/**
	 * handles new customer request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/customer/new" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/customer/save" })
	public String newCustomer(@Valid Customer customer,BindingResult bindingResult) {
		logger.info("new customer " + customer.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("customer", customer);
			return "NewCustomer";
		}

		// If data is ok, insert and go on
		customerBO.insert(customer);
		return "redirect:/customer";
	}

	/**
	 * handles delete customer, and redirect to customer default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/customer","/customer/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/customer/detail" })
	public ModelAndView customerDetail(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("Detail " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Customer customer = customerBO.selectById(id);
		model.put("customer", customer);

		return new ModelAndView("CustomerDetail", model);
	}

	/**
	 * handles customer update, first part: register loaded
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/customer/update" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/customer/update" })
	public ModelAndView update(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("update " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Customer customer = customerBO.selectById(id);
		model.put("customer", customer);
		return new ModelAndView("UpdateCustomer", model);

	}

	/**
	 * handles save update customer request. In the jsp we are using spring's
	 * form:form tags Spring expects a model called by default 'command' or any
	 * name we set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/customer/saveUpdate" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/customer/saveupdate" })
	public String saveUpdate(@Valid Customer customer,BindingResult bindingResult) {
		logger.info("update customer " + customer.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("customer", customer);
			return "UpdateCustomer";
		}

		customerBO.update(customer);
		return "redirect:/customer";
	}
}
